<template>
  <div class="login" :style="{ bgColor: 'red' }">
    <el-form :model="ruleForm" ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" validate>
      <el-form-item prop="username">
        <el-input type="text" v-model="ruleForm.username" clearable placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input type="password" v-model="ruleForm.password" show-password clearable placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item class="el-form-item-text">
        <el-button type="primary" @click="login()">登录</el-button>
      </el-form-item>
    </el-form>
</div>
</template>

<script>
import { userlogin } from "../request/api";
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 8 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          userlogin(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //登陆之后 存储用户信息
              // console.log(res);
              this.$store.dispatch('changeUserAction', res.list)
              this.$router.push("/index");


            } else {
              this.$message.error(res.msg);
            }
          });
        }
        return;
      });
    },
  },
};
</script>
<style lang="less" scoped>

</style>